sha256-uint8array
Fast SHA-256 digest hash based on Uint8Array, pure JavaScript.
SYNOPSIS
import {createHash} from "sha256-uint8array";
const text = "";
const hex = createHash().update(text).digest("hex");
const data = new Uint8Array(0);
const hash = createHash().update(data).digest();
See TypeScript declaration
sha256-uint8array.d.ts
for detail.
CJS
Both ES Modules and CommonJS supported.
const {createHash} = require("sha256-uint8array");
COMPATIBILITY
It has a better compatibility with Node.js's crypto
module in its smaller footprint.
The W3C standard crypto.subtle.digest()
API has a different interface which
returns
Promise<ArrayBuffer>
.
SPEED
It runs well both on Node.js and browsers.
Node.js's native crypto
module definitely runs faster than any others on Node.js, though.
The benchmark above shows milliseconds for 20,000 times of
SHA-256 hex
hash digest generation for approx 1KB string as input.
It is tested on macOS 10.15.7 Intel Core i7 3.2GHz.
You could run the benchmark as below.
git clone https://github.com/kawanet/sha256-uint8array.git
cd sha256-uint8array
npm install
npm run build
REPEAT=10000 ./node_modules/.bin/mocha test/99.benchmark.js
make -C browser
open browser/test.html
WEB BROWSERS
<script src="https://cdn.jsdelivr.net/npm/sha256-uint8array/dist/sha256-uint8array.min.js"></script>
<script>
const text = "";
const hex = SHA256.createHash().update(text).digest("hex");
const data = new Uint8Array(0);
const hash = SHA256.createHash().update(data).digest();
</script>
BROWSERIFY
It works great with
browserify
via browser
property of package.json
of your app if you needs
crypto.createHash("sha256").update(data).digest("hex");
syntax only.
{
"browser": {
"crypto": "sha256-uint8array/dist/sha256-uint8array.min.js"
},
"devDependencies": {
"browserify": "^17.0.0",
"sha256-uint8array": "^0.10.0"
}
}
It costs only less than 4KB, whereas browserify
's default crypto
polyfill
costs more than 300KB huge even after minified.
const crypto = require("crypto");
const hash = crypto.createHash("sha256").update("").digest("hex");
LINKS
MIT LICENSE
Copyright (c) 2020-2023 Yusuke Kawasaki
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.